/**
 * Copyright ©2016-2020 northkingbpo Corporation, All Rights Reserved
 * http://www.northkingbpo.net/
 */
package com.north.king.profile.http.client.resttemplate.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.north.king.profile.http.client.resttemplate.common.ConstantVariable;
import com.north.king.profile.http.client.resttemplate.server.data.CreateConditionData;
import com.north.king.profile.http.client.resttemplate.server.multi.MultiClientDesignated;
import com.north.king.profile.http.client.resttemplate.server.remote.RedisClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StopWatch;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * class info
 *
 * @author hujia
 * @date 2018/11/6 07:26
 */
@RestController
@Slf4j
public class ClientController {
    private final RedisClient redisClient;
    private final CreateConditionData createConditionData;
    private final ObjectMapper jackson;

    public ClientController(RedisClient redisClient, ObjectMapper jackson, CreateConditionData createConditionData) {
        this.redisClient = redisClient;
        this.jackson = jackson;
        this.createConditionData = createConditionData;
    }

    @GetMapping("/list/push/designated/{method}/{number}/")
    public String listPushDesignated(@PathVariable int number, @PathVariable int method) throws Exception {
        Integer[][] maxNumberList = {{50, 80, 120, 200}, {20, 20, 20, 20}, {20, 10, 5, 2}, {10, 20, 30, 40}};
        if (method >= 4 | method < 0) {
            return "method value is error.";
        }
        StopWatch watch = new StopWatch();
        watch.start("designated-list-push");
        List<Integer> range = Arrays.asList(maxNumberList[method]);
        redisClient.listPush(number, range);
        watch.stop();
        log.info(watch.shortSummary());
        return watch.shortSummary();
    }


    @GetMapping("/data/recommended/designated/{repeatTime}")
    public String recommendedRangeRepeat(@PathVariable int repeatTime) throws Exception {
        createConditionData.clearUser();
        createConditionData.createUser();
        redisClient.begin(ConstantVariable.TITLE_SINGLE_CONSTANT +repeatTime);
        StopWatch watch = new StopWatch();
        for (int i = 0; i < repeatTime; ++i) {
            Map<String, String> conditionMap = createConditionData.conditionDesignated(i % 20);
            watch.start("request");
            redisClient.recommendedByPerson(conditionMap);
            watch.stop();
            //log.info(conditionMap.toString());
        }
        redisClient.end();
        log.info(watch.shortSummary());
        return watch.shortSummary();
    }


    @GetMapping("/multi/recommended/designated/{threadCount}")
    public void multiRecommendedDesignated(@PathVariable int threadCount) throws Exception {
        createConditionData.clearUser();
        createConditionData.createUser();
        redisClient.begin(ConstantVariable.TITLE_CONCURRENT + threadCount);
        for (int i = 0; i < threadCount; ++i) {
            Thread t = new MultiClientDesignated(redisClient, createConditionData, i);
            t.start();
        }
        redisClient.end();
    }


}
